package one.bugu.android.demon.ui.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LangK
 * Created Date 2018/1/15
 */
public class BottomNavigationBar extends LinearLayout {

    private OnTabSelectedListener tabSelectedListener;
    private long animationTime = 0;
    private List<BottomNavigationItem> list;
    private static Context context;

    private final String DefaultBackgroundColor = "#CCCCCC";
    private final String DefaultTextColor = "#333333";

    private int activeBackgroundColor;
    private int activeTextColor;
    private int defaultTextColor;
    private int defaultBackgroundColor;

    public BottomNavigationBar(Context context) {
        super(context);
        initView(context);
    }

    public BottomNavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        setOrientation(LinearLayout.HORIZONTAL);
    }


    /**
     * 设置按下动画执行时间
     *
     * @param time
     */
    public BottomNavigationBar setAnimationDuration(long time) {
        this.animationTime = time;
        return this;
    }

    /**
     * 设置按下的字体颜色
     *
     * @param color
     */
    public BottomNavigationBar setActiveTextColor(int color) {
        this.activeTextColor = color;
        return this;
    }


    /**
     * 添加子tab
     *
     * @param item
     * @return
     */
    public BottomNavigationBar addItem(BottomNavigationItem item) {
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(item);
        return this;
    }

    public void initialise() {
        if (list == null) {
            throw new RuntimeException("子Tab不能为空");
        }
        setWeightSum(list.size());
        for (int i = 0; i < list.size(); i++) {
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setLayoutParams(new LinearLayoutCompat.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setGravity(Gravity.CENTER);
            if (this.defaultBackgroundColor == 0) {
                linearLayout.setBackgroundColor(Color.parseColor(DefaultBackgroundColor));
            } else {
                linearLayout.setBackgroundColor(getColor(this.defaultBackgroundColor));
            }
            linearLayout.setTag(i);

            BottomNavigationItem item = list.get(i);
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new LinearLayoutCompat.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            imageView.setImageResource(item.imgSrc);
            linearLayout.addView(imageView);
            TextView textView = new TextView(context);
            textView.setText(item.title);
            textView.setLayoutParams(new LinearLayoutCompat.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            if (this.defaultTextColor == 0) {
                textView.setTextColor(Color.parseColor(DefaultTextColor));
            } else {
                textView.setTextColor(getColor(this.defaultTextColor));
            }
            linearLayout.addView(textView);
            linearLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = (int) v.getTag();
                    refreshTab(index);
                    BottomNavigationBar.this.tabSelectedListener.onTabSelected(list.get(index).tag);
                }
            });
            this.addView(linearLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
        }

    }


    private void refreshTab(int index) {
        for (int i = 0; i < list.size(); i++) {
            LinearLayout linearLayout = (LinearLayout) this.getChildAt(i);
            BottomNavigationItem item = list.get(i);
            if (i == index) {
                if (item.activtyImgSrc != 0) {
                    ((ImageView) linearLayout.getChildAt(0)).setImageResource(item.activtyImgSrc);
                }
                if (this.activeTextColor == 0) {
                    if (this.defaultTextColor == 0) {
                        ((TextView) linearLayout.getChildAt(1)).setTextColor(Color.parseColor(this.DefaultTextColor));
                    } else {
                        ((TextView) linearLayout.getChildAt(1)).setTextColor(getColor(this.defaultTextColor));
                    }
                } else {
                    ((TextView) linearLayout.getChildAt(1)).setTextColor(getColor(this.activeTextColor));
                }
                if (this.activeBackgroundColor == 0) {
                    if (this.defaultBackgroundColor == 0) {
                        linearLayout.setBackgroundColor(Color.parseColor(this.DefaultBackgroundColor));
                    } else {
                        linearLayout.setBackgroundColor(getColor(this.defaultBackgroundColor));
                    }
                } else {
                    linearLayout.setBackgroundColor(getColor(this.activeBackgroundColor));
                }
            } else {
                ((ImageView) linearLayout.getChildAt(0)).setImageResource(item.imgSrc);
                if (this.defaultTextColor == 0) {
                    ((TextView) linearLayout.getChildAt(1)).setTextColor(Color.parseColor(this.DefaultTextColor));
                } else {
                    ((TextView) linearLayout.getChildAt(1)).setTextColor(getColor(this.defaultTextColor));
                }
                if (this.defaultBackgroundColor == 0) {
                    linearLayout.setBackgroundColor(Color.parseColor(this.DefaultBackgroundColor));
                } else {
                    linearLayout.setBackgroundColor(getColor(this.defaultBackgroundColor));
                }
            }
        }
        invalidate();

    }

    private int getColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return context.getColor(color);
        } else {
            return context.getResources().getColor(color);
        }
    }


    public BottomNavigationBar setActiveBackgroundColor(int color) {
        this.activeBackgroundColor = color;
        return this;
    }

    public BottomNavigationBar setDefaultBackgroundColor(int color) {
        this.defaultBackgroundColor = color;
        return this;
    }

    public BottomNavigationBar setDefaultTextColor(int color) {
        this.defaultTextColor = color;
        return this;
    }


    /**
     * 设置tab选择监听
     *
     * @param onTabSelectedListener
     */
    public void setTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        this.tabSelectedListener = onTabSelectedListener;
    }

    public void setPosition(int position) {
        this.tabSelectedListener.onTabSelected(list.get(position).tag);
        refreshTab(position);
    }

    public interface OnTabSelectedListener {
        public void onTabSelected(String tag);

        public void onTabReselected(int position);
    }

    public static class BottomNavigationItem {

        private String title;
        private int imgSrc;
        private int activtyImgSrc;
        private String tag;

        public BottomNavigationItem(int imgSrc, String title,String tag) {
            this.title = title;
            this.imgSrc = imgSrc;
            this.tag = tag;
        }

        public BottomNavigationItem(int imgSrc, int title,String tag) {
            this.title = context.getString(title);
            this.imgSrc = imgSrc;
            this.tag = tag;
        }

        public BottomNavigationItem setActivityImageResours(int imgSrc) {
            this.activtyImgSrc = imgSrc;
            return this;
        }


    }
}
